<?php
class FeedController extends Zend_Controller_Action
{
    protected $_content;
    protected $_tags;
    protected $_categories;

    public function init() {
        $this->_content    = new Application_Model_Content();
        $this->_tags       = new Application_Model_Tags();
        $this->_categories = new Application_Model_Categories();
        
        $this->_helper->_block->add('mainVideo');
        $this->_helper->_block->add('tabs');
        $this->_helper->_block->add('socialWidget');
        $this->_helper->_block->add('popularNews');
        $this->_helper->_block->add('announcements');
        $this->_helper->_block->add('sites');
        $this->_helper->_block->add('otherSites');
    }

    public function indexAction() {
        $this->_helper->redirector('news', 'feed');     
    }

    public function newsAction() {
        $categoryAlias = $this->_getParam('category_alias');
        $categoryId    = $this->_categories->getCategoryIdByAliasAndDepartmentId($categoryAlias);       
        $allContent    = $this->_content->getContentByCategoryId($categoryId);

        $this->view->content = $this->_content->makePreviewForFeed($allContent);          

        /*$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Array($content));
        $paginator->setCurrentPageNumber($this->_getParam('page', 1));
        $paginator->setItemCountPerPage(5); 
        $paginator->setPageRange(4);    */                  
        //$this->view->content      = $paginator;               
    }

    public function postAction() {
        //need for redirect after vk auth
        //$redirectUri = $this->getRequest()->getRequestUri();
        //setcookie('redirect_uri', $redirectUri);
        
        $alias = $this->_getParam('name');  
        //$modelComments = new Application_Model_Comments();
        //$vk = new My_Vk();

        $post = $this->_content->getPostByAlias($alias);
        if (!preg_match('/iframe|object/', $post['preview_mediadata'])
            && $post['preview_mediadata']) {
            $image = $post['preview_mediadata'];
            $post['preview_mediadata'] = '<img 
                                           src="/uploads/preview_images/'.$image.'" 
                                           alt="photo">
                                          </img>';
        }                
        $this->_content->visitContent($post['id']);             
        $post['tags'] = $this->_content->getPostTags($post['id']);

        /*$comments = $modelComments->getContentComments($post['id']);
        foreach ($comments as &$comment) {
            $params = array(
                'uid' => $comment['social_id'],
                'fields' => 'photo_50'
            );
            $response = $vk->request('users.get', $params);
            $comment['first_name'] = $response['response'][0]['first_name'];
            $comment['last_name']  = $response['response'][0]['last_name'];
            $comment['photo']      = $response['response'][0]['photo_50'];          
        } */      

        $this->view->popularTags  = $this->_tags->getPopularTags();
        $this->view->popularPosts = $this->_content->getPopularPosts();
        $this->view->post         = $post;
        /*$this->view->comments     = $comments;
        
        $identity = Zend_Auth::getInstance()->getIdentity();            
        if (property_exists($identity, 'socialId')) {
            $loginModel = new Application_Model_Login();
            $user = $loginModel->getUserBySocialId($socialId);
            if ($user) {
                $this->view->socialId = $socialId;
                $this->view->accessToken = $user['access_token'];
            } else {
                $this->view->authLink = $vk->getAuthUrl();  
            }
        } else {
            $this->view->authLink = $vk->getAuthUrl();          
        }     */  
    }

    public function postsByTagAction() {
        $tagId = $this->_getParam('tag_id');        
        $allTagContent = $this->_content->getContentByTagId($tagId);        

        $content = $this->_content->makePreviewForFeed($allTagContent);

        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Array($content));
        $paginator->setCurrentPageNumber($this->_getParam('page', 1));
        $paginator->setItemCountPerPage(4); 
        $paginator->setPageRange(4);            

        $this->view->popularTags  = $this->_tags->getPopularTags();
        $this->view->popularPosts = $this->_content->getPopularPosts();     
        $this->view->content      = $paginator;
    }
}